const ImageSource = require("../../utils/image_path.js");
const app=getApp()
var timer=null;
Page({
    data: {
        NavbarHeight:app.globalData.navBarHeight + app.globalData.statusBarHeight,
        WindowWidth:app.globalData.windowWidth,
        navigationSetting:{
            title:'DIY专区',
            model:0,
        },
        Data:[
        ],
        slideData:[],
        showId:0,
        dotMove:"",
        slideId:0,
        slideAnimated:[{},{},{},{}],
        ruleIsActive:0,
    },
    onLoad(){
        var baseUrl=ImageSource.Base+ImageSource.page[2];
        for(let i = 0; i <ImageSource.imageList[2].length; i++){
            ImageSource.imageList[2][i].imgSrc=baseUrl+ImageSource.imageList[2][i].imgSrc;
        }
        wx.cloud.getTempFileURL({
            fileList:ImageSource.imageList[2],
            success:res=>{
                for(let i = 0; i < ImageSource.imageList[2].length; i++){
                    ImageSource.imageList[2][i].imgSrc=res.fileList[i].tempFileURL;
                }
            }
        })
        console.log(ImageSource.imageList[2])
        this.setData({
            Data:ImageSource.imageList[2]
        })
        console.log(this.data.Data)
        let slideDataArray = new Array()
        let flag=this.data.Data[0].id%2;
        let id=1;
        for(let i=0;i<this.data.Data.length;i++){
            if(this.data.Data[i].id%2===flag){
                var a=Object.assign({"id":id},{"type":this.data.Data[i].type},{"background":this.data.Data[i].imgSrc});
                var b={"cover":this.data.Data[++i].imgSrc};
                var c=Object.assign(a,b);
                slideDataArray.push(c);
            }
        }
        let slideDataMap = new Map()
        slideDataArray.forEach(item => {
                //console.log(item);
                let type= item.type;
                if(!slideDataMap.has(type)) {
                    slideDataMap.set(type, [item])
                } else {
                    slideDataMap.get(type).push(item)
                }
            }
        )
        let slideData = [...slideDataMap.entries()];
        console.log(slideData);
        this.setData({
            slideData:slideData
        })
        console.log(slideData)
    },
    onShow(){
        this.slideChangeByTime();
    },
    slideChangeByTime(){
        var that = this;
        var index = 0;
        var times=0;
        timer = setTimeout(function fun() {
            var aniOut = wx.createAnimation({
                duration: 800,
                timingFunction: 'ease'
            })
            aniOut.opacity(0).step();
            var aniIn = wx.createAnimation({
                duration: 800,
                timingFunction: 'ease'
            })
            aniIn.opacity(1).step();
            const lat = ["animated[0]", "animated[1]", "animated[2]", "animated[3]"]
            that.setData({
                [lat[index]]: aniOut.export(),
            })
            index++;
            if (that.data.showId == 0)
                index = index % 4;
            else index = index % 3;
            that.setData({
                [lat[index]]: aniIn.export(),
            })
            times++;
            timer=setTimeout(fun,1000);
        }, 1000)
    },
    onHide(){
        clearTimeout(timer)
    },
    onUnload(){
        clearTimeout(timer)
    },
    showIdChangeItemTap(e){
        const index=e.currentTarget.dataset.index
        if(index===this.data.showId)return;
        this.dotMove(index);
        this.setData({
            showId:index
        })
    },
    dotMove(index){
        let move = wx.createAnimation({ duration:500 });
        let query = wx.createSelectorQuery();
        query.select('.switch0').boundingClientRect()
        query.exec((res)=> {
            let queryEnd = wx.createSelectorQuery();
            queryEnd.select('.switch'+index).boundingClientRect()
            queryEnd.exec((resc)=> {
                let end =(resc[0].right-resc[0].left)/2+resc[0].left;
                let start =(res[0].right-res[0].left)/2+res[0].left;
                let distance=end-start;
                move.translate(distance).step();
                this.setData({
                    dotMove:move.export()
                });
            })
        })
    },
    showMessageItemTap(){
        this.setData({
            ruleIsActive:1,
        })
    },
    skipItemTap(){
        wx.navigateTo({
            url:'../diyDesign/index?type='+this.data.showId,
        })
    },
    catchTouchMove(){
        return false;
    },
    closeRuleShowItemTap(){
        this.setData({
            ruleIsActive:0,
        })
    }

})
